/* Labyrinthe - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
import java.util.Random;

public class Labyrinthe implements ILaby
{
    public static final int UP = 0;
    public static final int RIGHT = 1;
    public static final int DOWN = 2;
    public static final int LEFT = 3;
    private Grid grille;
    private Position robotPos;
    private Position entry;
    private Position exit;
    private boolean ready = false;
    
	public Labyrinthe() {
	//NEW
	}
	
    private Labyrinthe(Position position, Position position_0_) {
	entry = new Position(position.row, position.col);
	robotPos = new Position(position.row, position.col);
	exit = new Position(position_0_.row, position_0_.col);
	//ready = false; //REMOVED
    }
	
	public void reset() {
	//NEW
	robotPos.row = entry.row;
	robotPos.col = entry.col;
	}
    
    public boolean isWall(int i) {
	if (!ready)
	    throw new RuntimeException("Labyrinthe not ready !");
	if (i < 0 || i > 4)
	    return true;
	if (i == 0)
	    return grille.isSet(robotPos.row - 1, robotPos.col);
	if (i == 2)
	    return grille.isSet(robotPos.row + 1, robotPos.col);
	if (i == 3)
	    return grille.isSet(robotPos.row, robotPos.col - 1);
	if (i == 1)
	    return grille.isSet(robotPos.row, robotPos.col + 1);
	return true;
    }
    
    public boolean go(int i) {
	if (!isWall(i)) {
	    if (i == 0)
		robotPos.row--;
	    else if (i == 2)
		robotPos.row++;
	    else if (i == 3)
		robotPos.col--;
	    else if (i == 1)
		robotPos.col++;
	    return true;
	}
	return false;
    }
    
    public int getRows() {
	return grille.getRows();
    }
    
    public int getCols() {
	return grille.getCols();
    }
    
    public boolean isExit() {
	if (!ready)
	    throw new RuntimeException("Labyrinthe not ready !");
	return robotPos.row == exit.row && robotPos.col == exit.col;
    }
    
    public void affichage() {
	char c = '#';
	if (ready) {
	    for (int i = 0; i < grille.getCols() + 2; i++)
		System.out.print(c);
	    System.out.println();
	    for (int i = 0; i < grille.getRows(); i++) {
		System.out.print(c);
		for (int i_1_ = 0; i_1_ < grille.getCols(); i_1_++) {
		    if (robotPos.row == i && robotPos.col == i_1_)
			System.out.print("x");
		    else if (entry.row == i && entry.col == i_1_)
			System.out.print("i");
		    else if (exit.row == i && exit.col == i_1_)
			System.out.print("o");
		    else if (grille.isSet(i, i_1_))
			System.out.print(c);
		    else
			System.out.print(" ");
		}
		System.out.println(c);
	    }
	    for (int i = 0; i < grille.getCols() + 2; i++)
		System.out.print(c);
	    System.out.println();
	}
    }
    
    private static boolean belongTo(Position[] positions, int i, int i_2_) {
	for (int i_3_ = 0; i_3_ < positions.length; i_3_++) {
	    Position position = positions[i_3_];
	    if (i == position.row && i_2_ == position.col)
		return true;
	}
	return false;
    }
    
    public static Labyrinthe laby1() {
	Position[] positions
	    = { new Position(2, 0), new Position(1, 0), new Position(0, 0),
		new Position(0, 1), new Position(0, 2), new Position(1, 2),
		new Position(2, 2), new Position(2, 3), new Position(2, 4),
		new Position(1, 4), new Position(0, 4), new Position(0, 5),
		new Position(0, 6), new Position(1, 6), new Position(2, 6),
		new Position(3, 6), new Position(4, 6), new Position(4, 5),
		new Position(4, 4), new Position(4, 3), new Position(4, 2),
		new Position(5, 2), new Position(6, 2), new Position(6, 3),
		new Position(6, 4), new Position(6, 5), new Position(6, 6),
		new Position(6, 7), new Position(5, 7) };
	return create(positions, 70, 8, 8);
    }
    
    public static Labyrinthe laby2() {
	Position[] positions
	    = { new Position(2, 1), new Position(2, 0), new Position(1, 0),
		new Position(0, 0), new Position(0, 1), new Position(0, 2),
		new Position(0, 3), new Position(1, 3), new Position(2, 3),
		new Position(3, 3), new Position(4, 3), new Position(4, 4),
		new Position(4, 5), new Position(5, 5), new Position(6, 5),
		new Position(6, 6), new Position(6, 7), new Position(5, 7),
		new Position(4, 7), new Position(3, 7), new Position(2, 7),
		new Position(1, 7), new Position(1, 8), new Position(1, 9) };
	return create(positions, 70, 8, 10);
    }
    
    public static Labyrinthe laby3() {
	Position[] positions
	    = { new Position(4, 5), new Position(4, 6), new Position(4, 7),
		new Position(4, 8), new Position(5, 8), new Position(6, 8),
		new Position(7, 8), new Position(8, 8), new Position(8, 7),
		new Position(8, 6), new Position(8, 5), new Position(8, 4),
		new Position(8, 3), new Position(8, 2), new Position(7, 2),
		new Position(6, 2), new Position(6, 1), new Position(6, 0),
		new Position(7, 0), new Position(8, 0), new Position(9, 0),
		new Position(10, 0), new Position(11, 0), new Position(11, 1),
		new Position(11, 2), new Position(11, 3), new Position(11, 4),
		new Position(11, 5), new Position(11, 6), new Position(11, 7),
		new Position(11, 8), new Position(11, 9), new Position(11, 10),
		new Position(11, 11), new Position(10, 11),
		new Position(9, 11), new Position(9, 12), new Position(9, 13),
		new Position(8, 13), new Position(7, 13), new Position(6, 13),
		new Position(5, 13), new Position(4, 13), new Position(3, 13),
		new Position(2, 13), new Position(1, 13), new Position(0, 13),
		new Position(0, 12), new Position(0, 11), new Position(1, 11),
		new Position(1, 10), new Position(1, 9), new Position(0, 9),
		new Position(0, 8), new Position(0, 7), new Position(0, 6),
		new Position(0, 5), new Position(0, 4), new Position(0, 3),
		new Position(0, 2), new Position(0, 1), new Position(1, 1) };
	return create(positions, 80, 12, 15);
    }
    
    public static Labyrinthe laby4() {
	Position[] positions
	    = { new Position(14, 18), new Position(14, 17),
		new Position(14, 16), new Position(14, 15),
		new Position(14, 14), new Position(13, 14),
		new Position(12, 14), new Position(11, 14),
		new Position(11, 15), new Position(11, 16),
		new Position(11, 17), new Position(10, 17),
		new Position(9, 17), new Position(8, 17), new Position(7, 17),
		new Position(6, 17), new Position(5, 17), new Position(4, 17),
		new Position(4, 16), new Position(4, 15), new Position(4, 14),
		new Position(4, 13), new Position(4, 12), new Position(4, 11),
		new Position(5, 11), new Position(6, 11), new Position(7, 11),
		new Position(7, 12), new Position(7, 13), new Position(8, 13),
		new Position(9, 13), new Position(9, 12), new Position(9, 11),
		new Position(9, 10), new Position(9, 9), new Position(10, 9),
		new Position(11, 9), new Position(12, 9), new Position(13, 9),
		new Position(13, 10), new Position(13, 11),
		new Position(14, 11), new Position(15, 11),
		new Position(15, 10), new Position(15, 9), new Position(15, 8),
		new Position(15, 7), new Position(15, 6), new Position(14, 6),
		new Position(13, 6), new Position(12, 6), new Position(12, 5),
		new Position(11, 5), new Position(10, 5), new Position(9, 5),
		new Position(8, 5), new Position(7, 5), new Position(6, 5),
		new Position(6, 6), new Position(6, 7), new Position(6, 8),
		new Position(5, 8), new Position(4, 8), new Position(3, 8),
		new Position(2, 8), new Position(2, 7), new Position(2, 6),
		new Position(2, 5), new Position(2, 4), new Position(2, 3),
		new Position(2, 2), new Position(2, 1), new Position(3, 1),
		new Position(4, 1), new Position(5, 1), new Position(6, 1),
		new Position(7, 1), new Position(7, 2), new Position(8, 2),
		new Position(9, 2), new Position(10, 2), new Position(11, 2),
		new Position(12, 2), new Position(12, 1), new Position(13, 1),
		new Position(14, 1), new Position(15, 1),
		new Position(15, 2) };
	return create(positions, 70, 16, 20);
    }
    
    private static Labyrinthe create(Position[] positions, int i, int i_4_,
				     int i_5_) {
	Labyrinthe labyrinthe
	    = new Labyrinthe(positions[0], positions[positions.length - 1]);
	labyrinthe.grille = new Grid(i_4_, i_5_);
	Random random = new Random();
	for (int i_6_ = 0; i_6_ < i_4_; i_6_++) {
	    for (int i_7_ = 0; i_7_ < i_5_; i_7_++) {
		if (!belongTo(positions, i_6_, i_7_)
		    && random.nextInt(100) < i)
		    labyrinthe.grille.set(i_6_, i_7_);
	    }
	}
	labyrinthe.ready = true;
	return labyrinthe;
    }
}
